//
//  GoodsBookViewController.m
//  Commerce
//
//  Created by gc on 15/3/28.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "GoodsBookViewController.h"
#import "AreaViewController.h"
#import "GoodsListViewController.h"

@interface GoodsBookViewController (){
    NSArray *_specificationArray;
    NSArray *_typeArray;
    NSDictionary *_startingDic;
    NSDictionary *_destinationDic;
}

@end

@implementation GoodsBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"预定期望流向";
    
    if (_isModify) {
        [_button setTitle:@"修 改" forState:UIControlStateNormal];
        _startingTextField.text = [NSString stringWithFormat:@"%@",_goodsDic[@"StartingCityName"]];
        _destinationTextField.text = [NSString stringWithFormat:@"%@",_goodsDic[@"DestinationCityName"]];
    }else{
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.navView.frame.size.height-38, 60, 30)];
        [rightBtn setTitle:@"已预定" forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"back_n.png"] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(gotoGoodsList:) forControlEvents:UIControlEventTouchUpInside];
        [self.navView addSubview:rightBtn];
    }
    [self getGoodsBookContent];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark -请求数据
- (void)getSpecificationData{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    NSLog(@"%@",parameters);
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sys/carspec",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  NSMutableArray *temp = [[NSMutableArray alloc] init];
                                  [temp addObject:@{@"Code":@"0",@"CodeID":@"0",@"CodeType":@"0",@"Text":@"不限制"}];
                                  [temp addObjectsFromArray:responseObject];
                                  _specificationArray = temp;
                                  [[NSUserDefaults standardUserDefaults] setObject:_specificationArray forKey:@"SpecificationData"];
                                  [SVProgressHUD dismiss];
                              }];
}

- (void)getTypeData{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    NSLog(@"%@",parameters);
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sys/cartype",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  NSMutableArray *temp = [[NSMutableArray alloc] init];
                                  [temp addObject:@{@"Code":@"0",@"CodeID":@"0",@"CodeType":@"0",@"Text":@"不限制"}];
                                  [temp addObjectsFromArray:responseObject];
                                  _typeArray = temp;
                                  [[NSUserDefaults standardUserDefaults] setObject:_typeArray forKey:@"TypeData"];
                                  [SVProgressHUD dismiss];
                              }];
}

- (void)getGoodsBookContent{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@/sch/myvip?api_key=%@&userid=%@",URL_ROOT,[[@"1234567890!@#" md5HexDigest] uppercaseString],[[GlobalData sharedGlobalData] userinfoDic][@"UserID"]]
                                    params:nil
                               requestMode:DGHTTPMethodGET
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  _titleLabel.text = [NSString stringWithFormat:@"    %@",responseObject[@"result"]];
                                  [SVProgressHUD dismiss];
                              }];

}

#pragma mark -IBAction
- (IBAction)gotoGoodsList:(id)sender{
    GoodsListViewController *vc = [[GoodsListViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    [self presentViewController:nav animated:YES completion:^{}];
}

- (IBAction)getGoodsBook:(id)sender{
    if (_startingTextField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写出发地"];
        return;
    }else if (_destinationTextField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写目的地"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    NSString *uid = [[GlobalData sharedGlobalData] userinfoDic][@"UserID"];
    NSString *url;
    if (_isModify) {
        [parameters addEntriesFromDictionary:@{@"userid":uid,
                                               @"Scheduleid":_goodsDic[@"ScheduleID"],
                                               @"start_address":_goodsDic[@"StartAddress"],
                                               @"end_address":_goodsDic[@"EndAddress"],
                                               }];
        url = [NSString stringWithFormat:@"%@sch/up",URL_ROOT];
        
    }else{
        [parameters addEntriesFromDictionary:@{@"userid":uid,
                                               @"start_address":_startingDic[@"AreaID"],
                                               @"end_address":_destinationDic[@"AreaID"],
                                               }];
        url = [NSString stringWithFormat:@"%@sch/new",URL_ROOT];
    }
    NSLog(@"%@",parameters);
    
    [[BaseNetwork alloc] initWithMethodUrl:url
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  [SVProgressHUD dismiss];
                                  
                                  if (_isModify) {
                                      if ([responseObject[@"result"] integerValue] == 1) {
                                          [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                                      }
                                      return;
                                  }
                                  if ([responseObject[@"result"] integerValue] == 1) {
                                      [self gotoGoodsList:nil];
                                  }else if ([responseObject[@"result"] integerValue] == -1){
                                      [SVProgressHUD showInfoWithStatus:@"已经创满了订阅器"];
                                  }else{
                                      [SVProgressHUD showInfoWithStatus:@"创建失败"];
                                  }
                              }];
}

- (IBAction)gotoStartingView:(id)sender{
    AreaViewController *vc = [[AreaViewController alloc] initWithNibName:nil bundle:nil];
    vc.typeNum = 1;
    vc.delegate =self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)gotoDestinationView:(id)sender{
    AreaViewController *vc = [[AreaViewController alloc] initWithNibName:nil bundle:nil];
    vc.typeNum = 2;
    vc.delegate =self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -AreaDelegate
- (void)showAreaData:(NSInteger)TypeNum andArea:(NSDictionary *)areaDic andAreaName:(NSString *)areaName{
    
    if (TypeNum == 1) {
        if (_isModify) {
            NSMutableDictionary *item = [[NSMutableDictionary alloc] initWithDictionary:_goodsDic];
            [item setObject:areaDic[@"AreaID"] forKey:@"StartAddress"];
            _goodsDic = item;
        }
        _startingDic = areaDic;
        _startingTextField.text = areaName;
    }else if (TypeNum == 2){
        if (_isModify) {
            NSMutableDictionary *item = [[NSMutableDictionary alloc] initWithDictionary:_goodsDic];
            [item setObject:areaDic[@"AreaID"] forKey:@"StartAddress"];
            _goodsDic = item;
        }
        _destinationDic = areaDic;
        _destinationTextField.text = areaName;
    }
}

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
